import frappe
from frappe import _

def get_permission_query_conditions(user):
    if not user:
        user = frappe.session.user
    # todos that belong to user or assigned by user
    roles = frappe.get_roles()    
    if "System Manager" in roles:
        return ""    
    return f"(`tabCP_Opportunity`.salesman = '{user}' or `tabCP_Opportunity`.owner_type = '公海')".format(user=frappe.db.escape(user))

def has_permission(doc, user=None, permission_type=None):
    roles = frappe.get_roles()    
    if "System Manager" in roles:
        return True    
    
    if permission_type == "read" and doc.owner_type == "公海":
        return True

    if permission_type == "read" and doc.salesman == user:
        return True

    return False